﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JeSequencer;
using System.Collections;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        StateMachine sm;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StateMachine tester = new StateMachine();
            //add to state
            tester.MyStates.addState(1, 1, "1");
            tester.MyStates.addState(2, 2, "2");
            //edit state 01,add change conditions to it
            tester.MyStates[1].AddCondition("a1>10", 2,"t11");
            tester.MyStates[1].AddCondition("a1<5", 2,"t12");
            //edit state 02
            tester.MyStates[2].AddCondition("a1>15", 1,"t21");
            tester.MyStates[2].AddCondition("a1<2", 1,"t22");
            //initialize inputlist,importent
            tester.InputList.Add("a1", "0");   //inputlist use both string as key and value
            //initial state
            tester.CurrentState = 1;
            //try save in xml
            tester.SaveToFile("d:\\jst.xml");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            sm = new StateMachine();
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
             sm = StateMachine.OpenFromFile("d:\\jst.xml");
            button2.Text = "aa";
            sm.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            sm.InputList["a1"] = textBox1.Text;
            sm.inputChanged();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            sm.UpdateState();
            label1.Text = sm.CurrentState.ToString()+",,"+sm.InputList["LIVETIME"].ToString();
        }

        private void button5_Click(object sender, EventArgs e)
        {
           //add st
            try
            {
                sm.MyStates.addState(Convert.ToInt16(TbStNo.Text), Convert.ToInt16(TbDefStNo.Text), tbStName.Text);
                listBox1.Items.Add(TbStNo.Text);
            }
            catch
            {
                sm.MyStates.RemoveState(Convert.ToInt16(listBox1.SelectedItem.ToString()));
                sm.MyStates.addState(Convert.ToInt16(TbStNo.Text), Convert.ToInt16(TbDefStNo.Text), tbStName.Text);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //remove st
            sm.MyStates.RemoveState(Convert.ToInt16(listBox1.SelectedItem.ToString()));
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //Add condi
            int stIndex=Convert.ToInt16((listBox1.Text));
            try
            {
                sm.MyStates[stIndex].AddCondition(TbExpr.Text, Convert.ToInt16(TbConiNs.Text), TbCondiName.Text);
                listBox2.Items.Add(TbCondiName.Text);
            }
            catch 
            {
                sm.MyStates[stIndex].RemoveConditon(listBox2.Text);
                sm.MyStates[stIndex].AddCondition(TbExpr.Text, Convert.ToInt16(TbConiNs.Text), TbCondiName.Text);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            //remove condi
            int stIndex = Convert.ToInt16((listBox1.Text));
            sm.MyStates[stIndex].RemoveConditon(listBox2.Text);
            listBox2.Items.RemoveAt(listBox2.SelectedIndex);
        }
        private void refreshCondi()
        {
            int stIndex = Convert.ToInt16((listBox1.Text));
            listBox2.Items.Clear();
            foreach (DictionaryEntry condi in sm.MyStates[stIndex].Conditions)
            {
                listBox2.Items.Add(((Condition)condi.Value).Name);
            }
        }

        private void refreshState()
        {
            
            listBox1.Items.Clear();
            foreach (State st in sm.MyStates.States_S)
            {
                listBox1.Items.Add(st.StateNo);
            }
            listBox1.SelectedIndex = 0;
        }

        private void refreshInput()
        {
            foreach(string ip in sm.InputlistKey)
            {
                listBox3.Items.Add(ip);
                listBox4.Items.Add(ip);
            }
        }

        private void displaySt()
        {
            try
            {
                int stIndex = Convert.ToInt16((listBox1.Text));
                tbStName.Text = sm.MyStates[stIndex].StateName;
                TbStNo.Text = sm.MyStates[stIndex].StateNo.ToString();
                TbDefStNo.Text = sm.MyStates[stIndex].DefaultNs.ToString();
            }
            catch
            { }
        }

        private void displayCondi()
        {
            try
            {
                int stIndex = Convert.ToInt16((listBox1.Text));
                TbCondiName.Text = ((Condition)(sm.MyStates[stIndex].Conditions[listBox2.Text])).Name;
                TbConiNs.Text = ((Condition)(sm.MyStates[stIndex].Conditions[listBox2.Text])).NextState.ToString();
                TbExpr.Text = ((Condition)(sm.MyStates[stIndex].Conditions[listBox2.Text])).ConditonExpr;
            }
            catch
            { }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            //add input
            try
            {
                listBox3.Items.Add(TbInput.Text);
                sm.InputList.Add(TbInput.Text, TbinnValue.Text);
            }
            catch
            {
                sm.InputList.Remove(TbInput.Text);
                sm.InputList.Add(TbInput.Text, TbinnValue.Text);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            displaySt();
            refreshCondi();
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            displayCondi();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            SaveFD.ShowDialog();
            string path = SaveFD.FileName;
            sm.CurrentState = Convert.ToInt16(listBox1.Text);
             //try save in xml
            sm.SaveToFile(path);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            //remove input
            sm.InputList.Remove(listBox3.Text);
            listBox3.Items.RemoveAt(listBox3.SelectedIndex);
        }

        private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button13_Click(object sender, EventArgs e)
        {
            //change input
            sm.InputList[listBox4.Text] = TbRealInput.Text;
            sm.inputChanged();
        }

        private void button14_Click(object sender, EventArgs e)
        {
            //update sm
            sm.UpdateState();
            LbSt.Text = sm.CurrentState.ToString();
            LbLive.Text  = sm.InputList["LIVETIME"].ToString();
        }

        private void button15_Click(object sender, EventArgs e)
        {
            sm.Start();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            //open
            openFD.ShowDialog();
            string path = openFD.FileName;
            sm = StateMachine.OpenFromFile(path);
            refreshInput();
            refreshState();
        }

        private void groupBox4_Enter(object sender, EventArgs e)
        {

        }

        private void button16_Click(object sender, EventArgs e)
        {
            int stIndex = Convert.ToInt16((listBox1.Text));
            sm.DefSt = stIndex;
        }

    }
}
